//
//  ModalViewController.m
//  NavBar
//
//  Created by Thiago on 23/07/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ModalViewController.h"
#import "NavBarAppDelegate.h"


@implementation ModalViewController

@synthesize statAttributes, contentArray, rightLabel, leftLabel;

//static NSMutableArray *statValues = nil;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	
	self.title = [[NSString alloc] initWithString:@"Your Stats"];
	
	UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Dismiss" style:UIBarButtonSystemItemCancel
																 target:self action:@selector(dissmissView:)];	
	self.navigationItem.rightBarButtonItem = cancelBtn;
	
	// Fill the tableview with the stat attribute array
	NavBarAppDelegate *delegate = (NavBarAppDelegate *) [ [ UIApplication sharedApplication] delegate];
	statAttributes = delegate.statAttrArray;
	contentArray = delegate.sectionContents;
	
	UITableView *tableView = [[UITableView alloc] init];
	tableView.delegate = self;
	tableView.dataSource = self;
	// tableView.scrollEnabled = NO;
	tableView.rowHeight = (CGFloat) 20.5;
	self.view = tableView;
	[tableView reloadData];
	[tableView release];
} 



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void)dissmissView:(id) sender {
	[self dismissModalViewControllerAnimated:YES];
}	

- (void)dealloc {
	[statAttributes release];
    [super dealloc];
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return [contentArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [[[contentArray objectAtIndex:section] valueForKeyPath:@"contents.@count"] intValue];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [[contentArray objectAtIndex:section] objectForKey:@"GroupName"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *kCellIdentifier = @"cellTitle";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
	
	if (!cell)
	{
		
		cell =	[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:kCellIdentifier] autorelease];
		// Default style for cell
		//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCellIdentifier] autorelease];
		
		// Stat attribute
		leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(2,1,140,19)];
		leftLabel.font = [UIFont boldSystemFontOfSize:15];
		[cell.contentView addSubview:leftLabel];
		
		
		// Stat value
		rightLabel = [[UILabel alloc] initWithFrame:CGRectMake( 144, 1, 174, 19)];
		rightLabel.textAlignment = UITextAlignmentRight;
		rightLabel.font = [UIFont boldSystemFontOfSize:15];
		rightLabel.textColor = [UIColor darkGrayColor];
		[cell.contentView addSubview:rightLabel];	
		
		// get the view controller's info dictionary based on the indexPath's row
		NSDictionary *section = [contentArray objectAtIndex:indexPath.section];
		NSArray *contents = [section valueForKey:@"contents"];	
		
		NSArray *chunks = [[contents objectAtIndex:indexPath.row] componentsSeparatedByString:@"|"];
		
		leftLabel.text = [chunks objectAtIndex:0];
		if ( [chunks count] > 1 ) rightLabel.text = [chunks objectAtIndex:1];
	}

	return cell;
	[leftLabel release];
	[rightLabel release];
}


#pragma mark -
#pragma mark UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   [self dismissModalViewControllerAnimated:YES];
}


@end
